package com.bigShen.mybatis.official.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : lei.shen
 * @date : 2018/11/21
 */
public class SqlSessionFactorySingleton {

    private static String resource = "mybatis-config.xml";
    private static SqlSessionFactory sqlSessionFactory = null;

    public static SqlSessionFactory getSqlSessionFactoryInstance() {

        if (null == sqlSessionFactory) {
            synchronized (SqlSessionFactorySingleton.class) {
                if (null == sqlSessionFactory) {
                    try {
                        InputStream is = Resources.getResourceAsStream(resource);
                        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return sqlSessionFactory;

    }

}
